package com.util;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 数据库工具类
 */
public class DBUtil {
    private static String dbURL = "jdbc:mysql://localhost:3306/bookmanagement";
    private static String UserName = "root";
    private static String Password = "123123";
    private static String JDBCName = "com.mysql.jdbc.Driver";

    public static Connection getConnection() throws Exception {
        Class.forName(JDBCName);
        Connection conn = DriverManager.getConnection(dbURL,UserName,Password);
        if(conn != null) {
            System.out.println("数据库连接成功");
        }
        return conn;
    }

    public void closeConnection(Connection conn) throws Exception {
        if(conn != null) {
            conn.close();
        }
    }

    public static void main(String[] args) {
        DBUtil dbUtil = new DBUtil();
        try {
            dbUtil.getConnection();
            System.out.println("数据库已连接");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }
}
